package cn.ronghuanet.gift.exception;

import cn.ronghuanet.gift.exception.code.ErrorCode;

import java.util.List;

public class GiftAssert {

    public final static String  DEFAULT_MESSAGE = "非法请求!请输入相关信息后再操作!";

    //提示是默认值得一个重载
    public static void assertNotNull(Object obj){
        if (obj==null)
            throw new BusinessException(DEFAULT_MESSAGE);
    }
    //断言对象不为null,为null报操作
    public static void assertNotNull(Object obj,String message){
        if (obj==null)
            throw new BusinessException(message);
    }
    public static void assertNotNull(Object obj,ErrorCode code){
        if (obj==null)
            throw new BusinessException(code.getMessage(),code.getCode());
    }

    //断言为null,不为null就报错
    public static void assertNull(Object obj,String message){
        if (obj!=null)
            throw new BusinessException(message);
    }


    //断言字符串不能为blank
    public static void assertStrNotBlank(String str,String message){
        if (str==null || str.trim().equals(""))
            throw new BusinessException(message);
    }

    //断言字符串为blank
    public static void assertStrBlank(String str,String message){
        if (str!=null || !str.trim().equals(""))
            throw new BusinessException(message);
    }


    //断言对象不为null,为null报操作
    public static void assertListNotNull(List<?> objs, String message){
        if (objs==null || objs.size()<1)
            throw new BusinessException(message);
    }

    //断言为null,不为null就报错
    public static void assertListNull(List<?> objs,String message){
        if (objs!=null && objs.size()>0)
            throw new BusinessException(message);
    }
    public static void assertListNull(List<?> objs,ErrorCode code){
        if (objs!=null && objs.size()>0)
            throw new BusinessException(code.getMessage(),code.getCode());
    }


    //断言字符串相等
    public static void assertStrEq(String str1,String str2,String message){
        assertNotNull(str1,message);
        assertNotNull(str2,message);

        if (!str1.equals(str2))
            throw new BusinessException(message);
    }
    public static void assertStrEq(String str1, String str2, ErrorCode code){
        assertNotNull(str1,code);
        assertNotNull(str2,code);

        if (!str1.equals(str2))
            throw new BusinessException(code.getMessage(),code.getCode());
    }

    public static void assertStrEqIgnoreCase(String str1,String str2,String message){
        assertNotNull(str1,message);
        assertNotNull(str2,message);

        if (!str1.equalsIgnoreCase(str2))
            throw new BusinessException(message);
    }

}
